package org.chen.common.lytrip.crawler;// CrawlerTask.java
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@TableName("crawler_task")
public class CrawlerTask {
    @TableId(type = IdType.AUTO)
    private Long id;
    
    private String taskName; // 任务名称
    private String keywords; // 爬取关键词，多个用逗号分隔
    private Integer startPageIndex; // 开始页码，默认1
    private Integer endPageIndex;   // 结束页码，如果为null则只爬取startPageIndex页
    private Integer pageSize; // 每页数量
    private String cronExpression; // cron表达式
    private Integer status; // 状态：1-启用，0-暂停，2-停用
    private Integer days = 1; // 几日游
    private String departureCity = "成都";
    private Integer totalRuns; // 总运行次数
    private Integer successRuns; // 成功次数
    private Integer failRuns; // 失败次数
    private LocalDateTime lastRunTime; // 上次执行时间
    private LocalDateTime nextRunTime; // 下次执行时间
    private String lastRunResult; // 上次执行结果
    private String lastErrorMessage;    // 最后错误信息
    private String executionSummary; // 执行摘要
    private String description; // 任务描述
    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    /**
     * 转换为调度任务对象
     */
    public CrawlerTask toScheduledTask() {
        CrawlerTask scheduledTask = new CrawlerTask();
        scheduledTask.setId(this.id);
        scheduledTask.setTaskName(this.taskName);
        scheduledTask.setKeywords(this.keywords);
        scheduledTask.setStartPageIndex(this.startPageIndex);
        scheduledTask.setEndPageIndex(this.endPageIndex);
        scheduledTask.setPageSize(this.pageSize);
        scheduledTask.setCronExpression(this.cronExpression);
        scheduledTask.setStatus(this.status);
        scheduledTask.setTotalRuns(this.totalRuns != null ? this.totalRuns : 0);
        scheduledTask.setSuccessRuns(this.successRuns != null ? this.successRuns : 0);
        scheduledTask.setFailRuns(this.failRuns != null ? this.failRuns : 0);
        scheduledTask.setLastRunTime(this.lastRunTime);
        scheduledTask.setLastRunResult(this.lastRunResult);
        scheduledTask.setLastErrorMessage(this.lastErrorMessage);
        scheduledTask.setExecutionSummary(this.executionSummary);
        scheduledTask.setDescription(this.description);
        scheduledTask.setCreateTime(this.createTime);
        scheduledTask.setUpdateTime(this.updateTime);
        scheduledTask.setDays(this.days);
        return scheduledTask;
    }

    /**
     * 计算成功率
     */
    public double getSuccessRate() {
        if (totalRuns == null || totalRuns == 0) {
            return 0.0;
        }
        return Math.round((double) successRuns / totalRuns * 10000) / 100.0; // 保留两位小数
    }
}